﻿using NxStoreManage;

using System;
using System.Data;

namespace LengthAiCalculate
{
    public partial class AddSumData : BaseForm
    {
        public AddSumData()
        {
            InitializeComponent();
            SetDataGridDefault(dataGridView1, rowHeadersVisible: false, autoGenerateColumns: true);
            SetDataGridDefault(dataGridView2, rowHeadersVisible: false, autoGenerateColumns: true);
            SetDataGridDefault(dataGridView3, rowHeadersVisible: false, autoGenerateColumns: true);
            dataGridView1.ContextMenuStrip = contextMenuStrip1;
            dataGridView2.ContextMenuStrip = contextMenuStrip2;
            dataGridView3.ContextMenuStrip = contextMenuStrip3;
        }

        private DataTable lineCodeDt;

        private void AddSumData_Load(object sender, EventArgs e)
        {
            lineCodeDt = sB_sqlHelper.GetDataTableFromSql($"SELECT DISTINCT s.[LineCode],p.[lineName] FROM SB_StoreOut s INNER JOIN [SB_p_Line] p ON [s].[LineCode] = [p].[lineCode] WHERE (DATEDIFF(DAY, s.SCRQ, GETDATE()) <=20) ORDER BY s.[LineCode] ASC");
            BindComboBox(comboBox1, lineCodeDt);
            BindComboBox(comboBox4, lineCodeDt);
            BindComboBox(comboBox6, lineCodeDt);
            //BindComboBox(comboBox4, sB_sqlHelper.GetDataTableFromSql($"SELECT DISTINCT s.[LineCode],p.[lineName] FROM SB_StoreOut s INNER JOIN [SB_p_Line] p ON [s].[LineCode] = [p].[lineCode] WHERE (DATEDIFF(DAY, s.SCRQ, GETDATE()) <=20) ORDER BY s.[LineCode] ASC"));
            //BindComboBox(comboBox4, sB_sqlHelper.GetDataTableFromSql($"SELECT DISTINCT s.[LineCode],p.[lineName] FROM SB_StoreOut s INNER JOIN [SB_p_Line] p ON [s].[LineCode] = [p].[lineCode] WHERE (DATEDIFF(DAY, s.SCRQ, GETDATE()) <=20) ORDER BY s.[LineCode] ASC"));
        }

        private DataTable GETKZHDt(string lineCode)
        {
            return sB_sqlHelper.GetDataTableFromSql($@"SELECT KeyCode,RTRIM([GGLBName]) + RTRIM([GGName]) + '(' + RTRIM(KZH) + ')' KZH FROM V_SB_StoreOut WHERE (DATEDIFF(DAY, SCRQ, GETDATE()) <= 20 AND [LineCode] = {lineCode}) ORDER BY [ID] DESC");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedValue != null)
                BindComboBox(comboBox2, GETKZHDt(comboBox1.SelectedValue.ToString()));
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedValue != null)
            {
                dataGridView1.DataSource = sB_sqlHelper.GetDataTableFromSql($@"SELECT ZZPH,
                   GGLB,
                   SteelClassName,
                   GGNAMETOEXCLE,
                   K_ZXBZ,
                   SUM(inRealNum) inRealNum,
                   SUM(cutRealNum) cutRealNum,
                   SUM(jzNum) jzNum
            FROM V_SB_StoreOutTraceOnLine
            WHERE OutKeyCode = '{comboBox2.SelectedValue}'
                  AND inRealNum > 0
            GROUP BY ZZPH,
                     GGLB,
                     SteelClassName,
                     GGNAMETOEXCLE,
                     K_ZXBZ
            ORDER BY ZZPH ASC;");
                SetDg1();
            }
        }

        private void SetDg1()
        {
            dataGridView1.Columns[0].HeaderText = "轧制批号";
            dataGridView1.Columns[1].HeaderText = "规格类别";
            dataGridView1.Columns[2].HeaderText = "钢种";
            dataGridView1.Columns[3].HeaderText = "规格";
            dataGridView1.Columns[4].HeaderText = "执行标准";
            dataGridView1.Columns[5].HeaderText = "热轧支数";
            dataGridView1.Columns[6].HeaderText = "热锯支数";
            dataGridView1.Columns[7].HeaderText = "矫直支数";
            dataGridView1.Columns[0].Width = 100;
            dataGridView1.Columns[1].Width = 100;
            dataGridView1.Columns[2].Width = 100;
            dataGridView1.Columns[3].Width = 120;
            dataGridView1.Columns[4].Width = 150;
            dataGridView1.Columns[5].Width = 80;
            dataGridView1.Columns[6].Width = 80;
            dataGridView1.Columns[7].Width = 80;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.FilterData($"ZZPH LIKE '%{textBox1.Text.Trim()}%'");
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox4.SelectedValue != null)
                BindComboBox(comboBox3, GETKZHDt(comboBox4.SelectedValue.ToString()));
        }

        private AddCutGroupItem addCutGroupItem = null;

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                ShowError("请先选择一行数据");
                return;
            }
            if (addCutGroupItem == null || addCutGroupItem.IsDisposed)
            {
                addCutGroupItem = new AddCutGroupItem();
            }
            addCutGroupItem.SetInfo(comboBox2.SelectedValue.ToString(),
            dataGridView1.SelectedRows[0].Cells["ZZPH"].Value.ToString(),
            dataGridView1.SelectedRows[0].Cells["SteelClassName"].Value.ToString(),
            dataGridView1.SelectedRows[0].Cells["GGNAMETOEXCLE"].Value.ToString(),
            dataGridView1.SelectedRows[0].Cells["GGLB"].Value.ToString(), "0");
            SetShowFormDefault(addCutGroupItem);
            addCutGroupItem.SucAction = S2;
            addCutGroupItem.ShowDialog();
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            S2();
        }

        private void S2()
        {
            if (comboBox3.SelectedValue != null)
                dataGridView2.DataSource = nxsqlHelper.GetDataTableFromSql($@"SELECT [ZZPH],
                       [GGLB],
                       [steelClassName],
                       [sizeName],
                       CONVERT(FLOAT,[length] *1.0/ 1000) [length],
                       [cutNum],[packNum],inStoreNum,cutNum - inStoreNum lastNum,[itemKey],[id]
                FROM [PM.ManualManufactureResultGroupZZPH]
                WHERE outKeyCode='{comboBox3.SelectedValue}'");
            SetDg2();
        }

        private void SetDg2()
        {
            dataGridView2.Columns[0].HeaderText = "轧制批号";
            dataGridView2.Columns[1].HeaderText = "规格类别";
            dataGridView2.Columns[2].HeaderText = "纲级";
            dataGridView2.Columns[3].HeaderText = "规格";
            dataGridView2.Columns[4].HeaderText = "长度";
            dataGridView2.Columns[5].HeaderText = "锯切支数";
            dataGridView2.Columns[6].HeaderText = "包装支数";
            dataGridView2.Columns[7].HeaderText = "入库支数";
            dataGridView2.Columns[8].HeaderText = "在制品";
            dataGridView2.Columns[0].Width = 100;
            dataGridView2.Columns[1].Width = 100;
            dataGridView2.Columns[2].Width = 100;
            dataGridView2.Columns[3].Width = 120;
            dataGridView2.Columns[4].Width = 70;
            dataGridView2.Columns[5].Width = 80;
            dataGridView2.Columns[6].Width = 80;
            dataGridView2.Columns[7].Width = 80;
            dataGridView2.Columns[8].Width = 80;
            dataGridView2.Columns[9].Visible = false;
            dataGridView2.Columns[10].Visible = false;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            dataGridView2.FilterData($"ZZPH LIKE '%{textBox2.Text.Trim()}%'");
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count == 0)
            {
                ShowError("请先选择一行数据");
                return;
            }
            if (addCutGroupItem == null || addCutGroupItem.IsDisposed)
            {
                addCutGroupItem = new AddCutGroupItem();
            }
            addCutGroupItem.SetInfo(comboBox3.SelectedValue.ToString(),
            dataGridView2.SelectedRows[0].Cells["ZZPH"].Value.ToString(),
            dataGridView2.SelectedRows[0].Cells["steelClassName"].Value.ToString(),
            dataGridView2.SelectedRows[0].Cells["sizeName"].Value.ToString(),
            dataGridView2.SelectedRows[0].Cells["GGLB"].Value.ToString(),
            dataGridView2.SelectedRows[0].Cells["length"].Value.ToString());
            SetShowFormDefault(addCutGroupItem);
            addCutGroupItem.SucAction = S2;
            addCutGroupItem.ShowDialog();
        }

        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox6.SelectedValue != null)
                BindComboBox(comboBox5, GETKZHDt(comboBox6.SelectedValue.ToString()));
        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            S3();
        }

        private void S3()
        {
            if (comboBox5.SelectedValue != null)
                dataGridView3.DataSource = nxsqlHelper.GetDataTableFromSql($@"SELECT z.outKeyCode,
       z.sizeName,
       z.steelClassName,
       CONVERT(FLOAT, z.length / 1000.0) length,
       SUM(z.cutNum) AS cutNum,
       SUM(z.packNum) AS packNum,
       SUM(z.inStoreNum) AS inStoreNum,
       SUM(z.cutNum - z.packNum) AS lastNum,
       ISNULL(SUM(o.orderNum), 0) orderNum,
       CASE
           WHEN SUM(z.cutNum) - ISNULL(SUM(o.orderNum), 0) < 0 THEN
               0
           ELSE
               SUM(z.cutNum) - ISNULL(SUM(o.orderNum), 0)
       END ycNum,
       CASE
           WHEN SUM(z.cutNum) - ISNULL(SUM(o.orderNum), 0) < 0 THEN
               0
           ELSE
               CONVERT(DECIMAL(18, 2), (SUM(z.cutNum) - ISNULL(SUM(o.orderNum), 0)) * 1.0 / SUM(z.cutNum) * 1.0 * 100)
       END ycP,ISNULL(SUM(o.orderNum), 0) - SUM(z.cutNum) orderCNum
FROM dbo.[v.PM.ManufactureResultSum] z
    LEFT JOIN [v.PM.AiOutKeyCode] o
        ON z.outKeyCode = o.outKeyCode
           AND z.[length] = o.lg
           AND z.steelClassName = o.planSteel
           AND z.sizeName = o.planSize
WHERE z.outKeyCode = '{comboBox5.SelectedValue}'
GROUP BY z.[outKeyCode],
         z.[sizeName],
         z.[steelClassName],
         z.[length];");
            SetDg3();
        }

        private void SetDg3()
        {
            dataGridView3.Columns[0].HeaderText = "单号";
            dataGridView3.Columns[0].Visible = false;
            dataGridView3.Columns[1].HeaderText = "规格";
            dataGridView3.Columns[2].HeaderText = "钢级";
            dataGridView3.Columns[3].HeaderText = "长度";
            dataGridView3.Columns[4].HeaderText = "锯切";
            dataGridView3.Columns[5].HeaderText = "包装";
            dataGridView3.Columns[6].HeaderText = "入库";
            dataGridView3.Columns[7].HeaderText = "在制品";
            dataGridView3.Columns[8].HeaderText = "订单";
            dataGridView3.Columns[9].HeaderText = "余材";
            dataGridView3.Columns[10].HeaderText = "余材率%";
            dataGridView3.Columns[11].HeaderText = "订单欠";
            dataGridView3.Columns[1].Width = 100;
            dataGridView3.Columns[2].Width = 80;
            dataGridView3.Columns[3].Width = 70;
            dataGridView3.Columns[4].Width = 70;
            dataGridView3.Columns[5].Width = 70;
            dataGridView3.Columns[6].Width = 70;
            dataGridView3.Columns[7].Width = 70;
            dataGridView3.Columns[8].Width = 70;
            dataGridView3.Columns[9].Width = 70;
            dataGridView3.Columns[10].Width = 70;
            dataGridView3.Columns[11].Width = 70;
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            S3();
        }

        private ChangeAbandonSmall changeAbandonSmall = new ChangeAbandonSmall();

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count == 0)
            {
                ShowError("请先选择一行数据");
                return;
            }
            if (changeLength == null || changeLength.IsDisposed)
            {
                changeLength = new ChangeLength();
            }
            changeAbandonSmall.SetInfo(dataGridView2.SelectedRows[0].Cells["id"].Value.ToString(),
            dataGridView2.SelectedRows[0].Cells["ZZPH"].Value.ToString(),
            dataGridView2.SelectedRows[0].Cells["steelClassName"].Value.ToString(),
            dataGridView2.SelectedRows[0].Cells["sizeName"].Value.ToString(),
            dataGridView2.SelectedRows[0].Cells["length"].Value.ToString(),
            dataGridView2.SelectedRows[0].Cells["cutNum"].Value.ToString());
            SetShowFormDefault(changeAbandonSmall);
            changeAbandonSmall.SucAction = S2;
            changeAbandonSmall.ShowDialog();
        }

        private ChangeLength changeLength = new ChangeLength();

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count == 0)
            {
                ShowError("请先选择一行数据");
                return;
            }
            if (changeLength == null || changeLength.IsDisposed)
            {
                changeLength = new ChangeLength();
            }
            changeLength.SetInfo(dataGridView2.SelectedRows[0].Cells["id"].Value.ToString(),
            dataGridView2.SelectedRows[0].Cells["ZZPH"].Value.ToString(),
            dataGridView2.SelectedRows[0].Cells["steelClassName"].Value.ToString(),
            dataGridView2.SelectedRows[0].Cells["sizeName"].Value.ToString(),
            dataGridView2.SelectedRows[0].Cells["length"].Value.ToString(),
            dataGridView2.SelectedRows[0].Cells["cutNum"].Value.ToString());
            SetShowFormDefault(changeLength);
            changeLength.SucAction = S2;
            changeLength.ShowDialog();
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            ShowError("开发中......");
        }
    }
}